<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>平台组件管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script type="text/javascript"
            src="../lib/vue/jquery.min.js"></script>
    <script type="text/javascript" src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script type="text/javascript" src="../js/global.js"></script>
    <script type="text/javascript" src="../js/store.js"></script>
    <script type="text/javascript" src="../js/cookies.js"></script>
    <script src="../lib/vue/vue.min.js"></script>
    <script src="../lib/vue/axios.min.js"></script>
</head>

<body id="developerAdd">
<div class="x-body layui-anim layui-anim-up">
    <form class="layui-form">
        <div class="layui-form-item" hidden>
            <div class="layui-input-inline">
                <input type="text" id="L_companyId" name="companyId" required=""
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属公司</label>
            <div class="layui-inline" style="width: 300px;">
                <input type="text" id="L_companyName" name="companyName" autocomplete="off" class="layui-input"
                       readonly>
            </div>
        </div>
        <div class="layui-form-item" hidden>
            <div class="layui-input-inline">
                <input type="text" id="L_sysId" name="sysId" required=""
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属系统</label>
            <div class="layui-inline" style="width: 300px;">
                <input type="text" id="L_sysName" name="sysName" autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_userName" class="layui-form-label">
                <span class="x-red">*</span>用户名
            </label>
            <div class="layui-input-inline" style="width: 300px;">
                <input type="text" id="L_userName" name="name" required=""
                       lay-verify="userName"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>非空且唯一
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_tel" class="layui-form-label">
                <span class="x-red">*</span>联系方式
            </label>
            <div class="layui-input-inline" style="width: 300px;">
                <textarea id="L_tel" name="telephone" required=""
                          lay-verify="tel" class="layui-textarea"></textarea>
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>非空
            </div>
        </div>
        <div class="layui-form-item">
            <label for='L_userFlag' class="layui-form-label">
                <span class="x-red">*</span>用户角色
            </label>
            <!--0:负责人 1:普通开发者-->
            <div class="layui-input-inline">
                <select id="L_userFlag" name="userFlag" lay-verify="userFlag">
                    <option value=''>--请选择角色--
                    <option value='1'>系统组件接入员
                    <option value='2'>系统负责人
                </select>
            </div>
        </div>
        <div class="layui-form-item" v-if="managerFlag==='3'">
            <label class="layui-form-label" style="width: 100px">
                <span class="x-red">*</span>负责组件
            </label>
            <div class="layui-input-inline" style="width: 400px;" id="L_componentIds">

            </div>
        </div>
        <div class="layui-form-item" style="padding-top: 100px">
            <label class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="add" lay-submit="">
                点击添加
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery
        let form = layui.form
            , layer = layui.layer
        $('#L_companyName').val(getCookie('companyName'))
        $('#L_companyId').val(parseInt(getCookie('companyId')))
        $("#L_sysName").val(getCookie('sysName'))
        $("#L_sysId").val(parseInt(getCookie('sysId')))
        form.render();
        let componentArray;
        //获取组件下拉表
        $.get(_hostUrl + '/cmsComponents', function (res) {
            if (res.responseCode != 200) {
                layer.msg(res.responseMsg, {icon: 2, time: 1500});
                return
            }
            componentArray = res.data;
            if (!componentArray || componentArray.length == 0) {
                layer.msg("还未创建组件信息!")
            } else {
                for (let i = 0; i < componentArray.length; i++) {
                    $('#L_componentIds').append("<input type='checkbox' lay-filter='componentIds' name='componentIds' value=" + res.data[i].id + " title =" + res.data[i].componentName + ">" + "<br/>")
                }
                $('#L_componentIds').val(parseInt(getUrlKey('id')));
                form.render();
            }
        })
        //自定义验证规则
        form.verify({
            userName: function (value) {
                if (value.length < 1) {
                    return '用户名不可为空'
                }
            },
            tel: function (value) {
                if (value.length < 1) {
                    return '联系方式不可为空'
                }
            }
        })
        let componentValue = "";
        //监控组件列表选择事件
        form.on('checkbox(componentIds)', function () {
            //拼接id
            componentValue = getCheckboxValue('componentIds')
        })

        //监听提交
        form.on('submit(add)', function (data) {
            data.field.componentIds = componentValue;
            $.post(_hostUrl + "/user", data.field, function (res) {
                if (res.responseCode == 201) {
                    layer.msg('添加用户成功', {
                        icon: 1,
                        time: 2000
                    }, function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name)
                        //关闭当前frame
                        parent.layer.close(index)
                        var table = parent.window.layui.table;
                        table.reload('userList')
                    });
                } else {
                    layer.msg(res.responseMsg, {icon: 2, time: 1500})
                }
            }, 'json')
            return false
        });
    })
</script>
<script>
    let vm = new Vue({
        el: '#developerAdd',
        data() {
            return {
                userType: 0
            }
        }, beforeMount() {
            this.userType = getCookie('userType');
        }
    })
</script>
</body>

</html>